

// Node 链接MongoDB 数据库 

const mongoose = require("mongoose")

const host = "localhost"
const port = 27017
const user = "zuozuomu"
const pwd = "missyou"
const dbname = "wh2107"

// const DB_CONN_STR = `mongodb://${host}:${port}/${dbname}`
const DB_CONN_STR = `mongodb://${user}:${pwd}@${host}:${port}/${dbname}`

console.log(DB_CONN_STR)
// 链接数据库 
mongoose.connect(DB_CONN_STR,{
  useNewUrlParser:true,
  useUnifiedTopology:true
},err=>{
  if(err){
    console.log('mongodb 连接失败');
  }else{
    console.log('mongodb 连接成功');
  }
})


/**
  * 连接成功
  */
 mongoose.connection.on('connected', function () {    
  console.log('Mongoose connection open to ' + DB_CONN_STR);  
});    

/**
* 连接异常
*/
mongoose.connection.on('error',function (err) {    
  console.log('Mongoose connection error: ' + err);  
});    

/**
* 连接断开
*/
mongoose.connection.on('disconnected', function () {    
  console.log('Mongoose connection disconnected');  
});  


// setTimeout(function(){
//   mongoose.disconnect(function(){
//       console.log("断开连接");
//   })
// }, 2000);